'use strict';

goog.provide('Blockly.Blocks.mago');

goog.require('Blockly.Blocks');

// Color
// Blockly.Blocks.mago.HUE = 195;
var magoColor_base = '#406398';  //基础
var magoColor_music = '#DB8CCD'; //音乐
var magoColor_dis = '#51acff';   //显示
var magoColor_time = '#EC6E68';  //时钟
var magoColor_car = '#406398';   //电机驱动
var magoColor_ps2 = '#47C5CA';   //PS2遥控
// 未使用色彩
//ED157A - FF66B6
//E5B646 - FFCC66

/********************************************
BASE - 基础模块
*********************************************/


// PWM
Blockly.Blocks.mg_pwm = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_ANALOGWRITE)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_pwm.png", 71, 32))
    this.appendValueInput("PIN", Number)
      .appendField(Blockly.MIXLY_PIN)
      .setCheck(Number);
    this.appendValueInput("NUM", Number)
      .appendField(Blockly.MIXLY_VALUE2)
      .setCheck(Number);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip(Blockly.MIXLY_TOOLTIP_INOUT_ANALOG_WRITE);
  }
};

// Buzzer active - 有源蜂鸣器
Blockly.Blocks.mg_buzzer = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_YF_BUZZER)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_buzzer_p.png", 50, 32));
    this.appendValueInput("PIN", Number)
      .appendField(Blockly.MIXLY_PIN)
      .setCheck(Number);
    this.appendValueInput("STAT")
      .appendField(Blockly.MIXLY_STAT)
      .setCheck([Number, Boolean]);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
  }
};

// 级联显示OLED
Blockly.Blocks.mg_oleds_init = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_oled.png", 50, 32))
      .appendField("OLED级联地址初始化");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
  }
};
Blockly.Blocks.mg_oleds_port = {
  init: function () {
    this.setColour(magoColor_base);

    this.appendValueInput('port')
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("端口");
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip();
  }
};
//oled分区域显示字符
Blockly.Blocks.mg_oleds_cursor = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendValueInput('XVALUE')
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(Blockly.MIXLY_DISPLAY_MATRIX_X);
    this.appendValueInput("YVALUE")
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(Blockly.MIXLY_DISPLAY_MATRIX_Y);
    this.appendValueInput("TEXT", String)
    .setCheck([Number, String])
    .setAlign(Blockly.ALIGN_RIGHT)
    .appendField(Blockly.Msg.texttodisplay);
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip();
  }
};

//DHT11 - 温湿度传感器
Blockly.Blocks.mg_dht = {
  init: function () {
    var WHAT = [[Blockly.MIXLY_DHT11_T, 'temperature'], [Blockly.MIXLY_DHT11_H, 'humidity']];
    this.setColour(magoColor_base);
    this.appendValueInput("PIN", Number)
      .appendField("T&H")
      .appendField(new Blockly.FieldDropdown([[{ 'src': '../../media/mago/mg_dht11.png', 'width': 50, 'height': 32, 'alt': 'DHT11' }, '11'],
      [{ 'src': '../../media/mago/yf_dht22.png', 'width': 54, 'height': 32, 'alt': 'DHT22' }, '22']]), 'TYPE')
      .appendField(Blockly.MIXLY_PIN)
      .setCheck(Number);
    this.appendDummyInput("")
      .appendField(new Blockly.FieldDropdown(WHAT), "WHAT");
    this.setOutput(true, Number);
    var thisBlock = this;
    this.setTooltip(function () {
      var op = thisBlock.getFieldValue('WHAT');
      var TOOLTIPS = {
        'temperature': Blockly.MIXLY_TOOLTIP_BLOCKGROUP_GET_TEM,
        'humidity': Blockly.MIXLY_TOOLTIP_BLOCKGROUP_GET_HUM
      };
      return TOOLTIPS[op];
    });
  }
};

//微动开关
Blockly.Blocks.mg_microswitch = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_MICROSWITCH)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_microSwitch.png", 44, 32));
    this.appendValueInput("PIN", Number)
      .appendField(Blockly.MIXLY_PIN)
      .setCheck(Number);
    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};




/********************************************
Arduino Shield 
* L298P 电机驱动模块
* PM-R3 多功能扩展板模块
*********************************************/

/*  Motor drive - L298P电机驱动模块
    为了方便小朋友使用，将最后参数 -255 ~ 255 分割成两个部分：正反转 + 转速*/
var YF_MS = [
  [{ 'src': '../../media/mago/mago/mg_MA.png', 'width': 50, 'height': 26, 'alt': 'MA' }, '1'],
  [{ 'src': '../../media/mago/mago/mg_MB.png', 'width': 50, 'height': 26, 'alt': 'MB' }, '2'],
  [{ 'src': '../../media/mago/mago/mg_MC.png', 'width': 50, 'height': 26, 'alt': 'MC' }, '3'],
  [{ 'src': '../../media/mago/mago/mg_MD.png', 'width': 50, 'height': 26, 'alt': 'MD' }, '4']
];
// Motor direction - 电机转向
var YF_MD = [[Blockly.MIXLY_YFL298P_MOTOR_DIRECTION_F, '1'], [Blockly.MIXLY_YFL298P_MOTOR_DIRECTION_D, '0']];
var YF_MSPEED = [['慢速', '1'], ['中速', '2'], ['高速', '3']];

Blockly.Blocks.mg_Motor = {
  init: function () {
    this.setColour(magoColor_car);
    this.appendDummyInput()
      .appendField("核芯")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mg_logo.png", 55, 50))
      .appendField(Blockly.MIXLY_YFL298P_MOTOR)
      .appendField(new Blockly.FieldDropdown(YF_MS), 'PIN');
    this.appendDummyInput()
      .appendField(" ")
      .appendField(new Blockly.FieldDropdown(YF_MD), 'DIR');
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  }
};
Blockly.Blocks.mg_Motor_pwm = {
  init: function () {
    this.setColour(magoColor_car);
    this.appendDummyInput()
      .appendField("核芯")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mg_logo.png", 55, 50))
      .appendField(Blockly.MIXLY_YFL298P_MOTOR)
      .appendField(new Blockly.FieldDropdown(YF_MS), 'PIN');
    this.appendDummyInput()
      .appendField(" ")
      .appendField(new Blockly.FieldDropdown(YF_MD), 'DIR');
    this.appendValueInput('speed')
      .setCheck(Number)
      .appendField("转速");
    this.appendDummyInput()
      .appendField("(-255-255)");
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  }
};

Blockly.Blocks.mg_Motor_stop = {
  init: function () {
    this.setColour(magoColor_car);
    this.appendDummyInput("")
      .appendField("核芯")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mg_logo.png", 55, 50))
      .appendField(Blockly.MIXLY_YFL298P_MOTOR)
      .appendField(new Blockly.FieldDropdown(YF_MS), 'PIN')
      .appendField(" ")
      .appendField(Blockly.MIXLY_STOP);
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  }
};


/********************************************
more - 更多
*********************************************/
//红外测温
Blockly.Blocks.mg_Tempe = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("GY-906")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_gy-906.png", 60, 50));

    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};


//MAX30102心率监测
Blockly.Blocks.mg_MAX30102_init = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("MAX30102心率监测")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_max30102.png", 60, 50));;

    this.appendValueInput("PIN1")
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("SCL")
      .appendField(Blockly.MIXLY_PIN);

    this.appendValueInput("PIN3")
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("SDA")
      .appendField(Blockly.MIXLY_PIN);


    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};

Blockly.Blocks.mg_varspeedservo = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_SERVO)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField(Blockly.MIXLY_PIN)
      .appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN");

    this.appendValueInput("DEGREE", Number)
      .setCheck(Number)
      .appendField(Blockly.MIXLY_DEGREE_0_180);

    this.appendValueInput("SPEED", Number)
      .setCheck(Number)
      .appendField(Blockly.MIXLY_SPEED);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};
Blockly.Blocks.mg_varspeedservobywait = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("等待舵机移动完成")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField(Blockly.MIXLY_PIN)
      .appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN");

    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};
Blockly.Blocks.mg_varspeedservobystop = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("将舵机停在当前位置")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField(Blockly.MIXLY_PIN)
      .appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN");

    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};
Blockly.Blocks.mg_varspeedservobydegree = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_SERVO)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField(Blockly.MIXLY_PIN)
      .appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN")
      .appendField(Blockly.MIXLY_READ_DEGREES);

    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};

//LD3320语音模块添加指令
Blockly.Blocks.mg_LD3320_addCommand = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("LD3320语音模块")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_ld3320.png", 60, 50));
    this.appendValueInput("TEXT", String)
      .setCheck([Number, String])
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("指令拼音");
    this.appendValueInput('VALUE')
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("指令参数");
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip();
  }
};


//LD3320语音模块初始化
Blockly.Blocks.mg_LD3320_Init = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("LD3320语音模块初始化")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_ld3320.png", 60, 50));
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip();

  }
};
//LD3320语音模块开始识别
Blockly.Blocks.mg_LD3320_Start = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("LD3320语音模块开始识别")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_ld3320.png", 60, 50));
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip();

  }
};
Blockly.Blocks.mg_LD3320_getCommand = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("LD3320读取指令")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_ld3320.png", 60, 50));
    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};



//TODO json数据处理
Blockly.Blocks.mg_JSONParse = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendValueInput("A")
      /* .appendField(Blockly.MIXLY_SERVO)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50)) */
      .appendField("解析JSON格式文字");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};
Blockly.Blocks.mg_JSONSuccess = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      /* .appendField(Blockly.MIXLY_SERVO)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50)) */
      .appendField("已成功解析JSON格式?");


    this.setOutput(true, Boolean);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};
Blockly.Blocks.mg_JSON2Str = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendValueInput("STR")
      /* .appendField(Blockly.MIXLY_SERVO)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50)) */
      .appendField("取得JSON节点的值");


    this.setOutput(true, Boolean);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};

//点阵屏分区域显示字符
Blockly.Blocks.mg_display_Matrix_DrawPixel = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_MATRIX_TYPE)
      .appendField(new Blockly.FieldDropdown(MATRIX_TYPES), 'TYPE');
    // this.appendDummyInput("")
    //   .appendField(Blockly.MIXLY_MATRIX_NAME)
    //   .appendField(new Blockly.FieldTextInput('myMatrix'), 'matrixName');
    this.appendValueInput('XVALUE')
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(Blockly.MIXLY_DISPLAY_MATRIX_X);
    this.appendValueInput("XSTAT")
      .appendField(Blockly.MIXLY_STAT)
      .setCheck([Number, Boolean]);
    this.appendValueInput("YVALUE")
      .setCheck(Number)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(Blockly.MIXLY_DISPLAY_MATRIX_Y);
    this.appendValueInput("YSTAT")
      .appendField(Blockly.MIXLY_STAT)
      .setCheck([Number, Boolean]);
    this.appendValueInput("TEXT", String)
      .setCheck([Number, String])
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(Blockly.Msg.texttodisplay);
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip();
  }
};


//esp8266AP模式初始化
Blockly.Blocks.mg_WIFIAP_Init = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("esp8266AP模式初始化");
    // this.appendDummyInput("")
    //   .appendField(Blockly.MIXLY_MATRIX_NAME)
    //   .appendField(new Blockly.FieldTextInput('myMatrix'), 'matrixName');
    //this.appendDummyInput("")
    // .appendField(Blockly.MIXLY_4DIGITDISPLAY_TM1637_INIT)
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip();

  }
};

//检测WiFi是否连接
Blockly.Blocks.mg_checkwifiConnected = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput()
      .appendField("wifi已连接");
    this.setOutput(true, Number);
    this.setTooltip(Blockly.MIXLY_TOOLTIP_ETHERNET_CONNECTED);
  }
};


//擦除esp8266数据
Blockly.Blocks.mg_deleteesp8266Data = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("擦除esp8266数据");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip();

  }
};

Blockly.Blocks.mg_Blinker_ksljwl = {
  init: function () {
    this.appendDummyInput()
      .appendField("开始连接")
      .appendField(new Blockly.FieldTextInput("auth"), "privatekey");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip("通过AP模式连接WiFi,只需要auth");
    this.setHelpUrl("");
  }
};


Blockly.Blocks.mg_connectAPI = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendValueInput("URL")
      /* .appendField(Blockly.MIXLY_SERVO)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50)) */
      .appendField("获取API-url状态码");
    this.setOutput(true, Boolean);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};


Blockly.Blocks.mg_getAPIData = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("获取API返回内容");
    this.setOutput(true, Boolean);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};


//TODO 物联网mqtt
Blockly.Blocks.mg_MQTT_server = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(new Blockly.FieldImage("../../media/blynk/iot.png", 20, 20))
      .appendField(Blockly.MQTT_SERVER);
    this.appendValueInput("server_add")
      .appendField(Blockly.MQTT_SERVER_ADD)
      .setCheck(String);
    this.appendValueInput("server_port")
      .appendField(Blockly.MIXLY_ETHERNET_CLINET_PORT)
      .setCheck(Number);
    this.appendValueInput("Client_ID")
      .appendField(Blockly.MQTT_Client_ID)
      .setCheck(String);

    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setTooltip();
    this.setHelpUrl();
  }
};
Blockly.Blocks['MQTT_connect'] = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MQTT_connect);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip();
  }
};

//MQTT-发送消息到topic
Blockly.Blocks.mg_MQTT_publish = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(new Blockly.FieldImage("../../media/blynk/iot.png", 20, 20))
      .appendField(Blockly.MQTT_publish);
    this.appendValueInput("data");
    this.appendDummyInput("")
      .appendField(Blockly.LANG_MATH_RANDOM_INT_INPUT_TO);
    this.appendValueInput("Topic")
      .appendField(Blockly.MQTT_Topic)
      .setCheck(String);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setInputsInline(true);
    this.setTooltip(" ");
    this.setHelpUrl();
  }
};

Blockly.Blocks.MQTT_subscribe_value = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MQTT_Topic);
    this.appendValueInput("Topic");
    this.appendDummyInput("")
      .appendField(Blockly.Msg.HTML_VALUE)
    this.setInputsInline(true);
    this.setOutput(true, String);
  }
};


Blockly.Blocks['mg_MQTT_subscribe'] = {
  /**
   * Block for if/elseif/else condition.
   * @this Blockly.Block
   */
  init: function () {
    this.setColour(magoColor_base);
    this.appendValueInput('IF0')
      .setCheck(null)
      .appendField(Blockly.Msg.CONTROLS_IF_MSG_IF)
      .appendField(Blockly.MQTT_Topic)
      .appendField(Blockly.MQTT_subscribe2);
    this.appendStatementInput('DO0')
      .appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setMutator(new Blockly.Mutator(['controls_if_elseif',
      'controls_if_else']));
    // Assign 'this' to a variable for use in the tooltip closure below.
    var thisBlock = this;
    this.setTooltip(function () {
      if (!thisBlock.elseifCount_ && !thisBlock.elseCount_) {
        return Blockly.Msg.CONTROLS_IF_TOOLTIP_1;
      } else if (!thisBlock.elseifCount_ && thisBlock.elseCount_) {
        return Blockly.Msg.CONTROLS_IF_TOOLTIP_2;
      } else if (thisBlock.elseifCount_ && !thisBlock.elseCount_) {
        return Blockly.Msg.CONTROLS_IF_TOOLTIP_3;
      } else if (thisBlock.elseifCount_ && thisBlock.elseCount_) {
        return Blockly.Msg.CONTROLS_IF_TOOLTIP_4;
      }
      return '';
    });
    this.elseifCount_ = 0;
    this.elseCount_ = 0;
  }
};

Blockly.Blocks['mg_create_array2_with_text'] = {
  init: function() {
    this.setColour(Blockly.Blocks.lists.HUE);
    this.appendValueInput("name")
    .setCheck(null)
    .appendField(new Blockly.FieldDropdown(DATATYPES), "TYPE")
    .appendField("一维数组");
    this.appendValueInput("String")
    .setCheck(null)
    .appendField(Blockly.MIXLY_MAKELISTFROM );
    this.appendDummyInput()
    .appendField(Blockly.MIXLY_ESP32_SET);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setHelpUrl("");
  }
  
}


Blockly.Blocks.make_tool_normal = {
  init: function () {
    this.appendValueInput("data")
      .setCheck(null);
    this.setInputsInline(false);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip("");
    this.setHelpUrl("");
  }
};

//初始化舵机
Blockly.Blocks.make_arduino_servo_begin = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("连接舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("pin")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 到管脚#");
    this.setInputsInline(true);
    this.setOutput(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->初始化舵机，成功返回通道号，失败则返回0\n"
      + "2.语法:\n"
      + "->uint8_t attach(int pin)\n"
      + "3.参数:\n"
      + "->pin:所连接的控制管脚\n"
      + "4.返回值:uint8_t型数据"
    );
    this.setHelpUrl("");
  }
};

//初始化舵机 可设置最小角度和最大角度
Blockly.Blocks.make_arduino_servo_begin_1 = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("连接舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("pin")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 到管脚#");
    this.appendValueInput("min")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("最小脉宽");
    this.appendValueInput("max")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("微秒 最大脉宽");
    this.appendDummyInput()
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("微秒");
    this.setInputsInline(true);
    this.setOutput(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->初始化舵机，成功返回通道号，失败则返回0\n"
      + "2.语法:\n"
      + "->uint8_t attach(int pin, int min, int max)\n"
      + "3.参数:\n"
      + "->pin:所连接的控制管脚\n"
      + "->min:最小脉冲宽度\n"
      + "->max:最大脉冲宽度\n"
      + "4.返回值:uint8_t型数据"
    );
    this.setHelpUrl("");
  }
};

//舵机 设置角度
Blockly.Blocks.make_arduino_servo_write = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("value")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 角度设为");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度，不阻塞程序运行\n"
      + "2.语法:\n"
      + "->void write(int value)\n"
      + "3.参数:\n"
      + "->value:脉冲宽度或角度值\n"
      + "4.返回值:无"
    );
    this.setHelpUrl("");
  }
};

//舵机 设置角度和速度
Blockly.Blocks.make_arduino_servo_write_1 = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("value")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 角度设为");
    this.appendValueInput("speed")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("速度");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度，不阻塞程序运行\n"
      + "2.语法:\n"
      + "->void write(int value, uint8_t speed)\n"
      + "3.参数:\n"
      + "->value:脉冲宽度或角度值\n"
      + "->speed:从初始位置到目标位置之间的速度(0~255)，0时为全速\n"
      + "4.返回值:无"
    );
    this.setHelpUrl("");
  }
};

//舵机 设置角度和速度以及选择是否阻塞
Blockly.Blocks.make_arduino_servo_write_2 = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("value")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 角度设为");
    this.appendValueInput("speed")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("速度");
    this.appendValueInput("wait")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("等待完成?");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度\n"
      + "2.语法:\n"
      + "->void write(int value, uint8_t speed, bool wait)\n"
      + "3.参数:\n"
      + "->value:脉冲宽度或角度值\n"
      + "->speed:从初始位置到目标位置之间的速度(0~255)，0时为全速\n"
      + "->wait:是否阻塞程序的运行\n"
      + "4.返回值:无"
    );
    this.setHelpUrl("");
  }
};

//舵机 设置脉冲宽度
Blockly.Blocks.make_arduino_servo_writeMicroseconds = {
  init: function () {
    this.appendValueInput("value")
      .setCheck(null)
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name")
      .appendField(" 脉宽设为");
    this.appendDummyInput()
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("微秒");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 设置脉冲宽度，默认的最小脉宽为544微秒，最大脉宽为2400微秒\n"
      + "2.语法:\n"
      + "->void writeMicroseconds(int value)\n"
      + "3.参数:\n"
      + "->value:脉冲宽度\n"
      + "4.返回值:无"
    );
    this.setHelpUrl("");
  }
};

//舵机 获取数据
Blockly.Blocks.make_arduino_servo_read = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name")
      .appendField(new Blockly.FieldDropdown([["获取当前角度", "read"], ["获取当前脉宽", "readMicroseconds"], ["正在移动?", "isMoving"], ["舵机连上控制管脚?", "attached"]]), "type");
    this.setInputsInline(true);
    this.setOutput(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 获取当前角度、获取当前脉宽、正在移动?、舵机连上控制管脚?\n"
      + "2.语法:\n"
      + "->获取当前角度 - int read()\n"
      + "->获取当前脉宽 - int readMicroseconds()\n"
      + "->正在移动? - bool isMoving()\n"
      + "->舵机连上控制管脚? - bool attached()\n"
      + "3.参数:无\n"
      + "4.返回值:int或boolean型数据"
    );
    this.setHelpUrl("");
  }
};

//舵机 执行某些函数
Blockly.Blocks.make_arduino_servo_set = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name")
      .appendField(new Blockly.FieldDropdown([["停止移动", "stop"], ["等待移动完成", "wait"], ["停止执行动作组", "sequenceStop"], ["释放管脚", "detach"]]), "type");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 停止移动、等待移动完成、停止执行动作组、释放管脚\n"
      + "2.语法:\n"
      + "->停止移动 - void stop()\n"
      + "->等待移动完成 - void wait()\n"
      + "->停止执行动作组 - void sequenceStop()\n"
      + "->释放管脚 - void detach()\n"
      + "3.参数:无\n"
      + "4.返回值:无"
    );
    this.setHelpUrl("");
  }
};

//舵机 建立一个动作组
Blockly.Blocks.make_arduino_servo_action_group = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("创建动作组")
      .appendField(new Blockly.FieldTextInput("action_group"), "name");
    this.appendStatementInput("data")
      .setCheck(null);
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip("");
    this.setHelpUrl("");
  }
};

//舵机 动作组里数据定义
Blockly.Blocks.make_arduino_servo_action_group_data = {
  init: function () {
    this.appendDummyInput()
      .appendField("角度")
      .appendField(new Blockly.FieldTextInput("0"), "angle")
      .appendField(" 速度")
      .appendField(new Blockly.FieldTextInput("50"), "speed");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip("");
    this.setHelpUrl("");
  }
};

//舵机 循环执行某一动作组
Blockly.Blocks.make_arduino_servo_sequencePlay = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("sequencein")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 执行动作组");
    this.appendValueInput("numpositions")
      .setCheck(null)
      .appendField("动作数目");
    this.setInputsInline(true);
    this.setOutput(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 从索引为0的动作开始，循环执行某一动作组里的动作，返回数据的类型为uint8_t\n"
      + "2.语法:\n"
      + "->uint8_t sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions)\n"
      + "3.参数:\n"
      + "->sequenceIn:动作组\n"
      + "->numPositions:此动作组中动作的数目\n"
      + "4.返回值:uint8_t型数据"
    );
    this.setHelpUrl("");
  }
};

//舵机 执行某一动作组，可设置是否循环
Blockly.Blocks.make_arduino_servo_sequencePlay_1 = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("sequencein")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 执行动作组");
    this.appendValueInput("numpositions")
      .setCheck(null)
      .appendField("动作数目");
    this.appendValueInput("loop")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("循环动作组?");
    this.appendValueInput("startPos")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("开始动作索引");
    this.setInputsInline(true);
    this.setOutput(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 从某个索引的动作开始，执行某一动作组里的动作，返回数据的类型为uint8_t\n"
      + "2.语法:\n"
      + "->uint8_t sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions, bool loop, uint8_t startPos)\n"
      + "3.参数:\n"
      + "->sequenceIn:动作组\n"
      + "->numPositions:此动作组中动作的数目\n"
      + "->loop:循环执行动作组里动作或只执行动作组里某一索引的动作\n"
      + "->startPos:开始动作的索引值\n"
      + "4.返回值:uint8_t型数据"
    );
    this.setHelpUrl("");
  }
};

//for
Blockly.Blocks.make_for = {
  init: function () {
    this.appendValueInput("variate")
      .setCheck(null)
      .appendField("for(");
    this.appendValueInput("judge")
      .setCheck(null)
      .appendField(";");
    this.appendValueInput("operation")
      .setCheck(null)
      .appendField(";");
    this.appendDummyInput()
      .appendField(")");
    this.appendStatementInput("do")
      .setCheck(null)
      .appendField("执行");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip("for循环");
    this.setHelpUrl("");
  }
};

// PS2 Controller init
Blockly.Blocks.mg_ps2_init = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_YF_PS2)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_ps2_val/yf_ps2x.png", 60, 37))
      .appendField(Blockly.MIXLY_SETUP)
    this.appendValueInput("PIN1")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("DAT")
    this.appendValueInput("PIN2")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("CMD")
    this.appendValueInput("PIN3")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("CS")
    this.appendValueInput("PIN4")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("CLK")
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};


//PS2手柄键值 - button 数字
var MIXLY_YF_PS2_BTN = [
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_start.png', 'width': 32, 'height': 20, 'alt': 'yf_ps2_start' }, 'PSB_START'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_select.png', 'width': 32, 'height': 21, 'alt': 'yf_ps2_select' }, 'PSB_SELECT'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_up.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_up' }, 'PSB_PAD_UP'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_right.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_right' }, 'PSB_PAD_RIGHT'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_left.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_left' }, 'PSB_PAD_LEFT'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_down.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_down' }, 'PSB_PAD_DOWN'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_cross.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_cross' }, 'PSB_CROSS'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_circle.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_circle' }, 'PSB_CIRCLE'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_square.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_square' }, 'PSB_SQUARE'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_triangle.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_triangle' }, 'PSB_TRIANGLE'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_l1.png', 'width': 73, 'height': 32, 'alt': 'yf_ps2_l1' }, 'PSB_L1'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_r1.png', 'width': 73, 'height': 32, 'alt': 'yf_ps2_r1' }, 'PSB_R1'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_l2.png', 'width': 73, 'height': 37, 'alt': 'yf_ps2_l2' }, 'PSB_L2'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_r2.png', 'width': 73, 'height': 37, 'alt': 'yf_ps2_r2' }, 'PSB_R2'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_l3.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_l3' }, 'PSB_L3'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_r3.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_r3' }, 'PSB_R3'],
];
// var MIXLY_YF_PS2_BTN =[
//         ["PSB_START", "PSB_START"],
//         ["PSB_SELECT", "PSB_SELECT"],
//         ["PSB_PAD_UP", "PSB_PAD_UP"],
//         ["PSB_PAD_RIGHT", "PSB_PAD_RIGHT"],
//         ["PSB_PAD_LEFT", "PSB_PAD_LEFT"],
//         ["PSB_PAD_DOWN", "PSB_PAD_DOWN"],
//         ["PSB_CROSS", "PSB_CROSS"],
//         ["PSB_CIRCLE", "PSB_CIRCLE"],
//         ["PSB_SQUARE", "PSB_SQUARE"],
//         ["PSB_TRIANGLE", "PSB_TRIANGLE"],
//         ["PSB_L1", "PSB_L1"],
//         ["PSB_R1", "PSB_R1"],
//         ["PSB_L2", "PSB_L2"],
//         ["PSB_R2", "PSB_R2"],
//         ["PSB_L3", "PSB_L3"],
//         ["PSB_R3", "PSB_R3"],
//     ];

var MIXLY_YF_PS2_BTN_STA = [[Blockly.MIXLY_YF_PSB_PRESSED, "ButtonPressed"],
[Blockly.MIXLY_YF_PSB_RELEASED, "ButtonReleased"]];

// PS2 Controller button 
Blockly.Blocks.mg_ps2_btn = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_YF_PS2_BUTTON)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_ps2_val/yf_ps2x.png", 60, 37))
      .appendField(new Blockly.FieldDropdown(MIXLY_YF_PS2_BTN), "PS2_BTN")
      .appendField(Blockly.MIXLY_YF_PS2_BTN_STATUS)
      .appendField(new Blockly.FieldDropdown(MIXLY_YF_PS2_BTN_STA), "PS2_BTN_STATUS");
    this.setOutput(true, Number);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};

//PS2手柄摇杆
var MIXLY_YF_PS2_ROCKER_STA = [
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_lx.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_lx' }, 'PSS_LX'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_ly.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_ly' }, 'PSS_LY'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_rx.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_rx' }, 'PSS_RX'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_ry.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_ry' }, 'PSS_RY'],
];
// var MIXLY_YF_PS2_ROCKER_STA = [["PSS_LX", "PSS_LX"],["PSS_LY","PSS_LY"],["PSS_RX", "PSS_RX"],["PSS_RY","PSS_RY"]];

// PS2 Controller Rocker
Blockly.Blocks.mg_ps2_rocker = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_YF_PS2_ROCKER)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_ps2_val/yf_ps2x.png", 60, 37))
      .appendField(new Blockly.FieldDropdown(MIXLY_YF_PS2_ROCKER_STA), "PS2_ROCKER_STATUS");
    this.setOutput(true, Number);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};

//PS2手柄键值 - A button 模拟
var MIXLY_YF_PS2_A_BTN = [
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_up.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_up' }, 'PSAB_PAD_UP'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_right.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_right' }, 'PSAB_PAD_RIGHT'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_left.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_left' }, 'PSAB_PAD_LEFT'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_down.png', 'width': 40, 'height': 40, 'alt': 'yf_ps2_down' }, 'PSAB_PAD_DOWN'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_cross.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_cross' }, 'PSAB_CROSS'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_circle.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_circle' }, 'PSAB_CIRCLE'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_square.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_square' }, 'PSAB_SQUARE'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_triangle.png', 'width': 32, 'height': 32, 'alt': 'yf_ps2_triangle' }, 'PSAB_TRIANGLE'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_l1.png', 'width': 73, 'height': 32, 'alt': 'yf_ps2_l1' }, 'PSB_L1'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_r1.png', 'width': 73, 'height': 32, 'alt': 'yf_ps2_r1' }, 'PSB_R1'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_l2.png', 'width': 73, 'height': 37, 'alt': 'yf_ps2_l2' }, 'PSB_L2'],
  [{ 'src': '../../media/mago/mg_ps2_val/yf_ps2_r2.png', 'width': 73, 'height': 37, 'alt': 'yf_ps2_r2' }, 'PSB_R2'],

];
// var MIXLY_YF_PS2_A_BTN =[
//         ["PSAB_PAD_UP", "PSAB_PAD_UP"],
//         ["PSAB_PAD_RIGHT", "PSAB_PAD_RIGHT"],
//         ["PSAB_PAD_LEFT", "PSAB_PAD_LEFT"],
//         ["PSAB_PAD_DOWN", "PSAB_PAD_DOWN"],
//         ["PSAB_CROSS", "PSAB_CROSS"],
//         ["PSAB_CIRCLE", "PSAB_CIRCLE"],
//         ["PSAB_SQUARE", "PSAB_SQUARE"],
//         ["PSAB_TRIANGLE", "PSAB_TRIANGLE"],
//         ["PSAB_L1", "PSAB_L1"],
//         ["PSAB_R1", "PSAB_R1"],
//         ["PSAB_L2", "PSAB_L2"],
//         ["PSAB_R2", "PSAB_R2"],
//     ];

// PS2 Controller read analog value of the button  --- how hard you press the button
Blockly.Blocks.mg_ps2_a_btn = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_YF_PS2_A_BUTTON)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_ps2_val/yf_ps2x.png", 60, 37))
      .appendField(new Blockly.FieldDropdown(MIXLY_YF_PS2_A_BTN), "PS2_A_BTN");
    this.setOutput(true, Number);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};

// PS2 Controller read controller and setmotor
Blockly.Blocks.mg_ps2_readController_setMotoron = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_YF_PS2)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_ps2_val/yf_ps2x.png", 60, 37))
      .appendField("开启震动");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};
Blockly.Blocks.mg_ps2_readController_setMotoroff = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField(Blockly.MIXLY_YF_PS2)
      .appendField(new Blockly.FieldImage("../../media/mago/mg_ps2_val/yf_ps2x.png", 60, 37))
      .appendField("关闭震动");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};

//TODO 智慧家居

// rgb灯
Blockly.Blocks.mg_rgb2on = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("RGB流水灯点亮")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_rgb.png", 60, 37))
    this.appendValueInput("PIN1")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("灯数")
    this.appendValueInput("PIN2")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("间隔时间")
    this.appendDummyInput("")
      .appendField("毫秒(ms)")
    this.appendDummyInput("")
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(new Blockly.FieldColour("#99FF99"), "RGB_LED_COLOR");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};

Blockly.Blocks.mg_rgb2off = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("RGB流水灯熄灭")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_rgb.png", 60, 37))
    this.appendValueInput("PIN1")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("灯数")
    this.appendValueInput("PIN2")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("间隔时间")
    this.appendDummyInput("")
      .appendField("毫秒(ms)")
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};
Blockly.Blocks.mg_rgb2on_low = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("RGB灯呼吸效果(点亮)")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_rgb.png", 60, 37))
    this.appendValueInput("PIN1")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("灯数")
    this.appendDummyInput("")
      .appendField("颜色")
      .appendField(new Blockly.FieldDropdown([["红", "R"], ["绿", "G"], ["蓝", "B"], ["白", "W"]]), "COLOR")
      .appendField("渐变时间")
      .appendField(new Blockly.FieldDropdown([["慢速", "low"], ["中速", "middle"], ["快速", "fast"]]), "SPEED");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};
Blockly.Blocks.mg_rgb2off_low = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("RGB灯呼吸效果(熄灭))")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_rgb.png", 60, 37))
    this.appendValueInput("PIN1")
      .setCheck(Number).setAlign(Blockly.ALIGN_RIGHT)
      .appendField("灯数")
    this.appendDummyInput("")
      .appendField("颜色")
      .appendField(new Blockly.FieldDropdown([["红", "R"], ["绿", "G"], ["蓝", "B"], ["白", "W"]]), "COLOR")
      .appendField("渐变时间")
      .appendField(new Blockly.FieldDropdown([["慢速", "low"], ["中速", "middle"], ["快速", "fast"]]), "SPEED");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};

//水滴传感器
Blockly.Blocks.mg_raindrops = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("雨滴模块检测到水滴")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_raindrop.png", 60, 50));
    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};
//光照传感器
Blockly.Blocks.mg_lightcontrol = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("光控模块检测光线很暗")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_photo.png", 60, 50));
    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};
//触摸
Blockly.Blocks.mg_touchmodule = {
  init: function () {
    this.setColour(magoColor_base);
    this.appendDummyInput("")
      .appendField("触摸模块检测到手指")
      .appendField(new Blockly.FieldImage("../../media/mago/mago/mago_touch.png", 60, 50));
    this.setOutput(true, [Boolean, Number]);
    this.setInputsInline(true);
    this.setTooltip('');
  }
};


//舵机 设置角度和速度
Blockly.Blocks.mg_servo_write_smarthome = {
  init: function () {
    this.appendDummyInput()
      .appendField(new Blockly.FieldImage("../../media/mago/mago/servo.png", 60, 50))
      .appendField("舵机")
      .appendField(new Blockly.FieldTextInput("servo"), "name");
    this.appendValueInput("pin")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 到管脚#");
    this.appendValueInput("value")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField(" 角度设为");
    this.appendValueInput("speed")
      .setCheck(null)
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("速度");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(magoColor_base);
    this.setTooltip(
      "1.功能:\n"
      + "->舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度，不阻塞程序运行\n"
      + "2.语法:\n"
      + "->void write(int value, uint8_t speed)\n"
      + "3.参数:\n"
      + "->value:脉冲宽度或角度值\n"
      + "->speed:从初始位置到目标位置之间的速度(0~255)，0时为全速\n"
      + "4.返回值:无"
    );
    this.setHelpUrl("");
  }
};